package com.hiadechizi.dynamic.holder;

import com.hiadechizi.dynamic.enums.DataSourceEnum;

/**
 * @author: wangdejun
 * @create: 2019-11-18 16:36
 *
 * 数据源holder，用于保存当前线程的数据源
 *
 **/
public class DataSourceHolder {

    private static ThreadLocal<DataSourceEnum> dataSourceEnumThreadLocal = new ThreadLocal<>();


    /**
     * 设置当前线程的数据源
     *
     * @param dataSourceEnum
     */
    public static void set(DataSourceEnum dataSourceEnum) {
        dataSourceEnumThreadLocal.set(dataSourceEnum);
    }


    /**
     * 获取当前线程的数据源
     *
     * @return
     */
    public static DataSourceEnum get() {
        return dataSourceEnumThreadLocal.get();
    }


    /**
     * 清空当前线程的数据源，防止threadLocal出现内存泄漏
     *
     */
    public static void remove() {
        dataSourceEnumThreadLocal.remove();
    }
}
